package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.PhotoQueryObject;
import cn.wolfcode.luowowo.scenic.domain.Photo;
import cn.wolfcode.luowowo.scenic.domain.ScenicDetail;
import cn.wolfcode.luowowo.scenic.service.IPhotoService;
import cn.wolfcode.luowowo.scenic.service.IScenicDetailService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/photo")
public class PhotoController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IPhotoService photoService;
    @Reference
    private IScenicDetailService detailService;

    @RequestMapping("/list")
    public String listPhoto(@ModelAttribute("qo") PhotoQueryObject qo, Model model) {
        //scenic
        ScenicDetail scenicDetail = detailService.get(qo.getScenicId());
        model.addAttribute("scenic", scenicDetail);
        //toasts
        List<Destination> toasts = destinationService.listToasts(scenicDetail.getDestId());
        model.addAttribute("toasts", toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);
        //pageinfo
        PageInfo<Photo> pageInfo = photoService.queryByScenicId(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "photo/list";
    }

    @RequestMapping("/photos")
    public String listPhotos(@ModelAttribute("qo") PhotoQueryObject qo, Model model) {
        PageInfo<Photo> pageInfo = photoService.queryByScenicId(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "photo/photoTpl";
    }


    @RequestMapping("/detail")
    public String getPhoto(Long id, Model model) {
        //photo
        Photo photo = photoService.getById(id);
        model.addAttribute("entity", photo);
        //toasts
        List<Destination> toasts = destinationService.listToasts(photo.getDestId());
        model.addAttribute("toasts", toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);
        //scenic
        ScenicDetail scenicDetail = detailService.get(photo.getScenic().getId());
        model.addAttribute("scenic", scenicDetail);

        return "photo/detail";
    }
}
